package com.leyou.item.service;

import com.leyou.common.enums.ExceptionEnum;
import com.leyou.common.exceptions.LyException;
import com.leyou.common.utils.BeanHelper;
import com.leyou.item.dto.SpecGroupDTO;
import com.leyou.item.dto.SpecParamDTO;
import com.leyou.item.entity.SpecGroup;
import com.leyou.item.entity.SpecParam;
import com.leyou.item.mapper.SpecGroupMapper;
import com.leyou.item.mapper.SpecParamMapper;
import com.netflix.discovery.converters.Auto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

/**
 * @package com.leyou.item.service
 * @description:
 * @author: 86157
 * @date 2019/6/26 17:08
 * @version: V1.0
 */
@Service
public class SpecGroupService {

    @Autowired
    private SpecGroupMapper specGroupMapper;

    @Autowired
    private SpecParamMapper specParamMapper;

    public List<SpecGroupDTO> querySpecGroupByCid(Long cid) {
        SpecGroup specGroup = new SpecGroup();
        specGroup.setCid(cid);
        List<SpecGroup> list = specGroupMapper.select(specGroup);

        List<SpecGroupDTO> specGroupDTOS = BeanHelper.copyWithCollection(list, SpecGroupDTO.class);

        return specGroupDTOS;

    }

    public List<SpecParamDTO> querySpecParamByGid(Long gid, Long cid, Boolean searching) {
        //cid，gid，searching比选一个
        if (cid == null & gid == null & searching == null) {
            throw new LyException(ExceptionEnum.INVALID_PARAM_ERROR);
        }

        //查询条件
        SpecParam specParam = new SpecParam();
        specParam.setGroupId(gid);
        specParam.setCid(cid);
        specParam.setSearching(searching);
        List<SpecParam> list = specParamMapper.select(specParam);

        return BeanHelper.copyWithCollection(list, SpecParamDTO.class);
    }
}